package com.zlcx.tz.live.repository;

import com.zlcx.tz.live.domain.LiveRoomJoinRecord;
import com.zlcx.tz.live.vo.UserLiveStateVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * <p></p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @date Created in 2019/4/18 10:11
 * @copyright: Copyright (c) founders
 */
@Repository
public interface LiveRoomJoinRecordRepository extends JpaRepository<LiveRoomJoinRecord, Long>, JpaSpecificationExecutor<LiveRoomJoinRecord> {

    List<LiveRoomJoinRecord> findByUserIdAndJoinState(Long userId,Integer state);

    int countLiveRoomJoinRecordByRoomIdAndJoinStateAndUserRoleIn(Long roomId,Integer state,List<Integer> userRole);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update LiveRoomJoinRecord lrr set lrr.joinState=:joinState ,lrr.leaveTime=:now ,lrr.updateTime=:now " +
            "where lrr.roomId=:roomId and lrr.userId=:userId and lrr.joinState=1")
    int updateByRoomIdAndUserIdAndState(@Param("roomId")Long roomId,@Param("userId")Long userId,@Param("joinState")Integer joinState,@Param("now")Date now);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update LiveRoomJoinRecord lrr  set lrr.userRole=:userRole,lrr.updateTime=:now,lrr.roleChangeTime=:now " +
            "where lrr.roomId=:roomId and lrr.userId=:userId and lrr.joinState=1")
    int roleChange(@Param("roomId")Long roomId,@Param("userId")Long userId,@Param("userRole")Integer userRole,@Param("now")Date now);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update LiveRoomJoinRecord lrr  set lrr.userRole=:userRole,lrr.updateTime=:now " +
            "where lrr.roomId=:roomId and lrr.userId=:userId and lrr.joinState=1")
    int roleChangeRoomOwner(@Param("roomId")Long roomId,@Param("userId")Long userId,@Param("userRole")Integer userRole,@Param("now")Date now);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("update LiveRoomJoinRecord lrr  set lrr.userMicState=:userMicState,lrr.userSelfMicState=:userMicState,lrr.updateTime=:now" +
            " where lrr.roomId=:roomId and lrr.userId=:userId and lrr.joinState=1")
    int micStateChange(@Param("roomId")Long roomId,@Param("userId")Long userId,@Param("userMicState")Integer userMicState,@Param("now")Date now);

    List<LiveRoomJoinRecord> findByRoomIdAndJoinState(Long roomId, Integer joinState);

    List<LiveRoomJoinRecord> findByRoomId(Long roomId);

    LiveRoomJoinRecord findFirstByUserIdAndRoomIdAndJoinStateAndUserRole(Long userId,Long roomId,Integer joinState,Integer userRole);

    LiveRoomJoinRecord findFirstByUserIdAndRoomIdAndJoinState(Long userId,Long roomId,Integer joinState);

    LiveRoomJoinRecord findFirstByUserIdAndRoomIdOrderByRoleChangeTimeDesc(Long userId,Long roomId);

    LiveRoomJoinRecord findFirstByUserIdAndJoinStateOrderByRoleChangeTimeDesc(Long userId,Integer joinState);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomJoinRecord lrr SET lrr.userMicState =0 , lrr.updateTime =:updateTime WHERE lrr.roomId =:roomId AND lrr.userMicState=1 AND lrr.userId <> :hostUserId")
    int operateAllMicClose(@Param("updateTime")Date updateTime, @Param("roomId")Long roomId, @Param("hostUserId")Long hostUserId);

    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomJoinRecord lrr SET lrr.userMicState = lrr.userSelfMicState , lrr.updateTime =:updateTime WHERE lrr.roomId =:roomId AND lrr.userId <> :hostUserId")
    int operateAllMicOpen(@Param("updateTime")Date updateTime, @Param("roomId")Long roomId, @Param("hostUserId")Long hostUserId);



    @Transactional(rollbackOn=Exception.class)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE LiveRoomJoinRecord lrr SET lrr.joinState =:joinState , lrr.leaveTime =:updateTime , lrr.updateTime=:updateTime WHERE lrr.roomId =:roomId AND lrr.joinState=1")
    int exitAll(@Param("joinState")Integer joinState, @Param("updateTime")Date updateTime, @Param("roomId")Long roomId);


    @Query(value = "SELECT j.* FROM live_room_join_record j \n" +
            "LEFT JOIN live_room_info r ON r.id=j.room_id \n" +
            "WHERE j.join_state='1' AND r.room_state IN('3','4') ",nativeQuery = true)
    List<LiveRoomJoinRecord> getNotNonalQuitUser();

    LiveRoomJoinRecord findFirstByRoomIdAndUserRoleOrderByJoinStateAscRoleChangeTimeDesc(Long roomId,Integer userRole);

    @Query(value = "select new com.zlcx.tz.live.vo.UserLiveStateVO( l.id,  l.roomId,  r.roomName,  r.roomAvatar,  l.userId,  l.userRole,  l.enterTime,  l.leaveTime,  l.joinState) from " +
            "LiveRoomJoinRecord l , com.zlcx.tz.live.domain.LiveRoomInfo r " +
            "where  l.roomId = r.id and l.userRole = ?1 and l.joinState = ?2 and l.userId in (?3)")
    List<UserLiveStateVO> findByUserRoleAndJoinStateAndUserIdIn(Integer userRole, Integer joinState, List<Long> ids);

    LiveRoomJoinRecord findFirstByUserIdOrderByRoleChangeTimeDesc(Long userId);
}
